
package com.jf.cloud.product.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.util.PageUtil;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.i18n.I18nMessage;
import com.jf.cloud.common.product.constant.SpuType;
import com.jf.cloud.common.product.vo.SpuVO;
import com.jf.cloud.common.product.vo.app.SpuAppVO;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.product.mapper.SpuCollectionMapper;
import com.jf.cloud.product.model.SpuCollection;
import com.jf.cloud.product.service.SpuCollectionService;
import com.jf.cloud.product.service.SpuService;
import com.jf.cloud.product.vo.SpuCollectionVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 商品收藏信息
 *
 * @author zz
 * @date 2020-11-21 14:43:16
 */
@Service
public class SpuCollectionServiceImpl implements SpuCollectionService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());


    @Autowired
    private SpuCollectionMapper spuCollectionMapper;

    @Autowired
    private SpuService spuService;

    @Override
    public PageVO<SpuCollectionVO> page(PageDTO pageDTO) {
        return PageUtil.doPage(pageDTO, () -> spuCollectionMapper.list());
    }

    @Override
    public SpuCollectionVO getById(Long id) {
        return spuCollectionMapper.getById(id);
    }

    @Override
    public void save(SpuCollection spuCollection) {
        spuCollectionMapper.save(spuCollection);
    }

    @Override
    public void update(SpuCollection spuCollection) {
        spuCollectionMapper.update(spuCollection);
    }

    @Override
    public void deleteById(Long id) {
        spuCollectionMapper.deleteById(id);
    }

    @Override
    public SpuCollectionVO getBySpuId(Long spuId) {
        return null;
    }

    @Override
    public int userCollectionCount(Long spuId, Long userId) {
        return spuCollectionMapper.userCollectionCount(spuId, userId);
    }

    @Override
    public PageVO<SpuAppVO> getUserCollectionDtoPageByUserId(PageDTO pageDTO, Long userId, String spuName, Integer prodType) {
        PageVO<SpuAppVO> pageVO = PageUtil.doPage(pageDTO, () -> spuCollectionMapper.getUserCollectionDtoPageByUserId(userId, spuName, I18nMessage.getLang(),prodType));
        return pageVO;
    }

    @Override
    public void deleteBySpuIdAndUserId(Long spuId, Long userId) {
        spuCollectionMapper.deleteUserCollection(spuId, null, userId);
    }

    @Override
    public void spuBatchCollection(List<Long> spuIdList) {
        Long userId = AuthUserContext.get().getUserId();
        List<Long> spuIds = spuCollectionMapper.hasCollection(spuIdList, userId);
        spuIdList.removeAll(spuIds);
        if (CollectionUtil.isEmpty(spuIdList)) {
            return;
        }
        // 过滤活动商品
        List<SpuVO> spuVOList = spuService.getBySpuIds(spuIdList);
        for (SpuVO spuVO : spuVOList) {
            if (Objects.equals(spuVO.getSpuType(), SpuType.ACTIVE.value())) {
                logger.info("活动商品不可收藏，spuId:{}", spuVO.getSpuId());
                spuIdList.remove(spuVO.getSpuId());
            }
        }
        spuCollectionMapper.saveBatch(spuIdList, userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteBatchBySpuIdsAndUserId(List<Long> spuIds, Long userId) {
        boolean remove = false;
        for (Long spuId : spuIds) {
            if (Objects.isNull(spuService.getBySpuId(spuId))) {
                continue;
            }
            if (userCollectionCount(spuId, userId) > 0) {
                deleteBySpuIdAndUserId(spuId,userId);
                remove = true;
            }
        }
        return remove;
    }

    @Override
    public void deleteBySpuId(Long spuId) {
        spuCollectionMapper.deleteBySpuId(spuId);
    }

}
